<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *积分明细
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
);

//积分明细
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=2;
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['is_add'])){
		$params['is_add']=intval($_RQ['is_add']);
		$return['params']['is_add']=$_RQ['is_add'];
	}
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
		$return['params']['createtime']=$time_range;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','type','op_mid','is_add','plugin','createtime','fee','remark');
	$list=pdo_getslice('core_credit_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			if($item['is_add']==0){
				$item['fee']=-$item['fee'];
			}
			$item['op_midname']=pdo_getcolumn('core_users',array('id'=>$item['op_mid']),'name');
			$item['op_midname']=$item['op_midname']?:'无';
		}
	}
	
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'avatar','title'=>'用户头像','type'=>'image'),
			array('name'=>'nickname','title'=>'用户昵称','type'=>'string'),
			array('name'=>'fee','title'=>'余额变化','type'=>'string'),
			array('name'=>'remark','title'=>'备注','type'=>'string'),
			array('name'=>'op_midname','title'=>'操作员','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		)),
		'search'=>array(
			array('name'=>'is_add','width'=>10,'title'=>'操作类型','type'=>'radio','options'=>'is_add'),
			array('name'=>'createtime','width'=>15,'title'=>'发生时间','type'=>'time_range'),
			array('name'=>'mid','width'=>15,'title'=>'会员id','type'=>'choose','action'=>'core/member.get_member/'),
		)
	);
	$return['is_add']=array('减少','增加');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	$params['uniacid']=$_SESSION['uniacid'];
	$params['type']=2;
	if(!empty($_RQ['mid'])){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if(isset($_RQ['is_add'])){
		$params['is_add']=intval($_RQ['is_add']);
		$return['params']['is_add']=$_RQ['is_add'];
	}
	//创建时间
	if($_RQ['createtime']){
		$time_range=cfc('tools')->base64_decode($_RQ['createtime'],$_SESSION['base_key_str']);
		$time_range=json_decode($time_range,true);
		$params['createtime between']=array($time_range['tstart'],$time_range['tend']);
	}
	$list=pdo_getall('core_credit_log',$params);
	if($list){
		foreach($list as &$item){
			$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']));
			$item['nickname']=$member['nickname'];
			$item['avatar']=$member['avatar'];
			if($item['is_add']==0){
				$item['fee']=-$item['fee'];
			}
			$item['op_midname']=pdo_getcolumn('core_users',array('id'=>$item['op_mid']),'name');
			$item['op_midname']=$item['op_midname']?:'无';
			$item['createtime']=date('Y-m-d H:i:s',$item['createtime']);
		}
	}
	$header=array(
		'nickname'=>'用户昵称',
		'fee'=>'余额变化',
		'remark'=>'备注',
		'op_midname'=>'操作员',
		'createtime'=>'时间',
	);
	cfc('tools')->export2excel($header,$list,'余额日志');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('core_credit_log',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}